package com.ts.api.module.common.entity.vo;

import com.ts.api.module.common.entity.DistrictPO;
import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 地区编码返回前端实体
 */
@Data
public class RespDistrictVO {

    /** 省级 */
    private List<RespDistrictSimpleVO> listProvince;
    /** 市级 */
    private List<RespDistrictSimpleVO> listCity;
    /** 区县 */
    private List<RespDistrictSimpleVO> listCounty;

    public RespDistrictVO() {
        super();
        listProvince = new ArrayList<>();
        listCity = new ArrayList<>();
        listCounty = new ArrayList<>();
    }

    public void add(String key, RespDistrictSimpleVO respDistrictSimpleVO) {
        if ("1".equals(key)) {
            listProvince.add(respDistrictSimpleVO);
        }
        if ("2".equals(key)) {
            listCity.add(respDistrictSimpleVO);
        }
        if ("3".equals(key)) {
            listCounty.add(respDistrictSimpleVO);
        }
    }

    public static RespDistrictVO convertVO(List<DistrictPO> list) {
        RespDistrictVO districtVO = new RespDistrictVO();
        Map<String, List<DistrictPO>> collect = list.stream().collect(Collectors.groupingBy(t -> t.getType()));

        Iterator<Map.Entry<String, List<DistrictPO>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<DistrictPO>> next = iterator.next();
            next.getValue().stream().forEach(t -> districtVO.add(next.getKey(), new RespDistrictSimpleVO(t.getCode(), t.getName())));
        }
        return districtVO;
    }
}
